package com.ttpai.boot.lbs.util;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日历的操作
 * <p/>
 * Created by yokoboy on 2014/11/26.
 */
public class CalendarUtil {

    // ///////////////////////////////////////////// Day

    /**
     * 获得最近几天的数据，负数代表前几天，
     */
    public static Date lastDate(int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + n);// 让日期加1
        return calendar.getTime();
    }

    /**
     * 昨天
     */
    public static Date yesterday() {
        return lastDate(-1);
    }

    /**
     * 今天
     */
    public static Date today() {
        return new Date();
    }

    /**
     * 明天
     */
    public static Date tomorrow() {
        return lastDate(1);
    }

    // ///////////////////////////////////////////// Week

    /**
     * 获得最近几周的数据，负数代表前几周，
     */
    public static Date lastWeek(int n) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + (n * 7));// 让日期加1
        // calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) + n);//让日期加1
        return calendar.getTime();
    }

    /**
     * 本周的星期一
     */
    public static Date mondayOfThisWeek() {
        return mondayOfThisWeek(new Date());
    }

    /**
     * 日期所在周的星期一
     */
    public static Date mondayOfThisWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0) {
            dayofweek = 7;
        }
        calendar.add(Calendar.DATE, -dayofweek + 1);
        return calendar.getTime();
    }

    /**
     * 本周的星期日
     */
    public static Date sundayOfThisWeek() {
        return sundayOfThisWeek(new Date());
    }

    /**
     * 日期所在周的星期日
     */
    public static Date sundayOfThisWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0) {
            dayofweek = 7;
        }
        calendar.add(Calendar.DATE, -dayofweek + 7);
        return calendar.getTime();
    }

    // ///////////////////////////////////////////// Month

    /**
     * 获得最近几月的第一天，负数代表前几月，
     */
    public static Date lastMonthFirstDay(int n) {
        return lastMonthFirstDay(new Date(), n);
    }

    public static Date lastMonthLastDay(int n) {
        return lastMonthLastDay(new Date(), n);
    }

    /**
     * 获得最近几月的最有一天，负数代表前几月，
     */
    public static Date lastMonthFirstDay(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstDateOfMonth(date));
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + n);// 让日期加1
        return calendar.getTime();
    }

    public static Date lastMonthLastDay(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstDateOfMonth(date));
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + n);// 让日期加1
        return lastDateOfMonth(calendar.getTime());
    }

    /**
     * 本月最后一天
     */
    public static Date lastDateOfMonth() {
        return lastDateOfMonth(today());
    }

    /**
     * 所在月最后一天
     */
    public static Date lastDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    /**
     * 本月第一天
     */
    public static Date firstDateOfMonth() {
        return firstDateOfMonth(today());
    }

    /**
     * 所在月第一天
     */
    public static Date firstDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int days = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    // ///////////////////////////////

    /**
     * 当前时间是年中的第几周
     */
    public static int getWeekOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 当前时间的月份
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 当前时间的年份
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (getMonth(date) == 12 && getWeekOfYear(date) == 1) {
            return calendar.get(Calendar.YEAR) + 1;
        } else {
            return calendar.get(Calendar.YEAR);
        }
    }

    public static Date add(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + i);// 让日期加1
        return calendar.getTime();
    }

    public static Date add(Date date, int i, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(type, calendar.get(type) + i);// 让日期加1
        return calendar.getTime();
    }

    public static Date addMonth(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.MONTH) + i);// 让日期加1
        return calendar.getTime();
    }

    public static Date addMonth(String month, int i) {
        Date date = DateUtil.format(month, "yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + i);// 让日期加1
        return calendar.getTime();
    }

    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static Date sqlTimestampToDate(Timestamp timestamp) {
        return new Date(timestamp.getTime());
    }

}
